package cn.stronglink.assetmanage.business.inspection.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.Repair;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;

/**
 * Created by zhengjinglan on 2018/6/18.
 */

public class RegisterListAdapter extends  RecyclerView.Adapter<RegisterListAdapter.ViewHolder>{
    private Context mContext;
    private List<Repair> datas;
    private onRecyclerListener listener;

    public RegisterListAdapter(Context context){
        this.mContext=context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_repair_list,parent,false);
        return new RegisterListAdapter.ViewHolder(view,listener);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Repair repair = datas.get(position);
        holder.bindRepo(repair, position);
    }

    @Override
    public int getItemCount() {
        return datas==null?0:datas.size();
    }

    public void setOnItemClickListener(onRecyclerListener listener){
        this.listener = listener;
    }

    public void notifyDatasChanged(List<Repair> data){
        this.datas =  data;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView tv_repair_id;
        private onRecyclerListener listener;
        public ViewHolder(View itemView,onRecyclerListener listener) {
            super(itemView);
            this.listener = listener;
            tv_repair_id = (TextView)itemView.findViewById(R.id.tv_repair_id);
        }

        public void bindRepo(Repair repo, int position){
            tv_repair_id.setText(repo.getId_());
        }
        @Override
        public void onClick(View v) {
            if (listener != null){
                listener.onItemClicked(v, getAdapterPosition());
            }
        }
    }
}
